import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:xim/widget/dash_chat_2/dash_chat_2.dart';
import 'package:xim/screen/page/select_location_page.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
import 'package:xim/common/util/getx.dart';

import '../page/chat_detail_page_controller.dart';

class ChatBottomActionPanelController {

  /// panel 高度
  final _height = 0.0.obs;

  double get height => _height.value;

  double setHeight(double height) => _height.value = height;
}

/// 聊天框：底部操作面板
class ChatBottomActionPanel extends StatelessWidget {

  var chatController = Get.findOrInject(builder: () => ChatDetailPageController());

  late BuildContext context;

  ChatBottomActionPanel({Key? key, required double height}) : super(key: key) {
    controller.setHeight(height);
  }


  Future<List<XFile>> pickerImages() async {
    final ImagePicker picker = ImagePicker();
    var images =  await picker.pickMultiImage(requestFullMetadata: false);
    return images;
  }

  late List<Item> items = [
    Item(icon: InkWell(
      child: const Icon(Icons.image),
      onTap: (){
        pickerImages().then((files){
          if(files.isNotEmpty){
            for (var f in files) {
              chatController.sendMessage(ChatMessage(
                  user: chatController.state.currUser,
                  createdAt: DateTime.now(),
                  medias: [ChatMedia(url: f.path, type: MediaType.image, fileName: f.name)]
              ));
            }

          }
        });
      },
    ), tips: "相册"),
    Item(icon: InkWell(
      child: const Icon(Icons.camera_alt_outlined),
      onTap: () async {
        CameraPicker.pickFromCamera(context).then((value){
          var file = value?.file;
          if(file!=null) {
            // https://pub.dev/packages/wechat_camera_picker
          }
        });
      },
    ), tips: "相机"),
    Item(icon: const Icon(Icons.video_camera_back_outlined), tips: "视频通话"),
    Item(icon: InkWell(
      child: const Icon(Icons.location_on),
      onTap: (){
        Get.to(SelectLocationPage());
      },
    ), tips: "位置"),
    Item(icon: const Icon(Icons.keyboard_voice), tips: "语音输入"),
    Item(icon: const Icon(Icons.file_copy), tips: "文件"),
    Item(icon: const Icon(Icons.monetization_on), tips: "转账"),
    Item(icon: const Icon(Icons.music_note_outlined), tips: "音乐"),
  ];

  var controller = Get.findOrInject(builder: () => ChatBottomActionPanelController());

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return Obx(() {
      return SizedBox(
        height: controller.height,
        child: GridView.count(
          crossAxisCount: 4,
          children: List.generate(items.length, (index) => Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              items[index].icon,
              Text(items[index].tips, style: const TextStyle(fontSize: 12),)
            ],
          )),
        ),
      );
    });
  }
}

class Item {

  final Widget icon;

  final String tips;

  Item({required this.icon, required this.tips});
}

